{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "43f9ce31",
   "metadata": {
    "id": "43f9ce31"
   },
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/docs/quick-tour/namespacing.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/docs/quick-tour/namespacing.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "artificial-devil",
   "metadata": {
    "id": "artificial-devil",
    "papermill": {
     "duration": 0.037678,
     "end_time": "2021-04-16T15:12:08.268491",
     "exception": false,
     "start_time": "2021-04-16T15:12:08.230813",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# Namespacing with Pinecone\n",
    "\n",
    "Namespacing is a feature in Pinecone that allows you to partition your data in an index. When you read from or write to a namespace in an index, you only access data in that particular namespace. Namespacing is useful when you want to reuse the same data processing pipeline but maintain strict separation between subsets of your data.\n",
    "\n",
    "If your use-case is one where you feel a temptation to create multiple indexes programatically, consider whether the sort of multitenancy provided by namespaces would be a better solution to isolate different parts of your data.\n",
    "\n",
    "For example, if you were building a movie recommender system, you could use namespacing to separate recommendations by genre. But if you need more flexibility in how you group and search records, putting genre information into metadata and using metadata filtering would probably be a better fit."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab928f49-67c6-4717-b0c6-47cb795763aa",
   "metadata": {},
   "source": [
    "# Prerequisites"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "emotional-lyric",
   "metadata": {
    "id": "emotional-lyric",
    "papermill": {
     "duration": 0.027173,
     "end_time": "2021-04-16T15:12:08.383073",
     "exception": false,
     "start_time": "2021-04-16T15:12:08.355900",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Install dependencies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "pleasant-transfer",
   "metadata": {
    "id": "pleasant-transfer",
    "papermill": {
     "duration": 15.880968,
     "end_time": "2021-04-16T15:12:24.293137",
     "exception": false,
     "start_time": "2021-04-16T15:12:08.412169",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pip install -qU pandas==2.2.3 pinecone==6.0.2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c246952",
   "metadata": {},
   "source": [
    "## Creating an Index\n",
    "\n",
    "We begin by instantiating an instance of the Pinecone client. To do this we need a [free API key](https://app.pinecone.io)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a331165c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from pinecone import Pinecone\n",
    "\n",
    "# Get API key at app.pinecone.io\n",
    "api_key = os.environ.get(\"PINECONE_API_KEY\") or \"PINECONE_API_KEY\"\n",
    "\n",
    "# Instantiate the client\n",
    "pc = Pinecone(api_key=api_key)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e39abbf1",
   "metadata": {},
   "source": [
    "### Creating a Pinecone Index\n",
    "\n",
    "When creating the index we need to define several configuration properties. \n",
    "\n",
    "- `name` can be anything we like. The name is used as an identifier for the index when performing other operations such as `describe_index`, `delete_index`, and so on. \n",
    "- `metric` specifies the similarity metric that will be used later when you make queries to the index.\n",
    "- `dimension` should correspond to the dimension of the dense vectors produced by your embedding model. In this quick start, we are using made-up data so a small value is simplest.\n",
    "- `spec` holds a specification which tells Pinecone how you would like to deploy our index. You can find a list of all [available providers and regions here](https://docs.pinecone.io/troubleshooting/available-cloud-regions).\n",
    "\n",
    "There are more configurations available, but this minimal set will get us started."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "UYgB1gef1Utk",
   "metadata": {
    "id": "UYgB1gef1Utk",
    "tags": [
     "parameters"
    ]
   },
   "outputs": [],
   "source": [
    "index_name = \"pinecone-namespacing\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2da6e7ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Delete the demo index if it already exists\n",
    "if pc.has_index(name=index_name):\n",
    "    pc.delete_index(index_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "balanced-housing",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "balanced-housing",
    "outputId": "8a0bcad7-ed5b-4fd5-897e-1534497aa278",
    "papermill": {
     "duration": 16.057888,
     "end_time": "2021-04-16T15:12:41.454202",
     "exception": false,
     "start_time": "2021-04-16T15:12:25.396314",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "    \"name\": \"pinecone-namespacing\",\n",
       "    \"metric\": \"euclidean\",\n",
       "    \"host\": \"pinecone-namespacing-dojoi3u.svc.aped-4627-b74a.pinecone.io\",\n",
       "    \"spec\": {\n",
       "        \"serverless\": {\n",
       "            \"cloud\": \"aws\",\n",
       "            \"region\": \"us-east-1\"\n",
       "        }\n",
       "    },\n",
       "    \"status\": {\n",
       "        \"ready\": true,\n",
       "        \"state\": \"Ready\"\n",
       "    },\n",
       "    \"vector_type\": \"dense\",\n",
       "    \"dimension\": 2,\n",
       "    \"deletion_protection\": \"disabled\",\n",
       "    \"tags\": null\n",
       "}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pinecone import ServerlessSpec, Metric, CloudProvider, AwsRegion\n",
    "\n",
    "# Create an index\n",
    "pc.create_index(\n",
    "    name=index_name,\n",
    "    dimension=2,\n",
    "    metric=Metric.EUCLIDEAN,\n",
    "    spec=ServerlessSpec(cloud=CloudProvider.AWS, region=AwsRegion.US_EAST_1),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "35cde5d7-efe5-47d6-aacf-1f0df6599d18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The index host is pinecone-namespacing-dojoi3u.svc.aped-4627-b74a.pinecone.io\n"
     ]
    }
   ],
   "source": [
    "# You can look up the index configuration for an existing\n",
    "# index using describe_index\n",
    "index_config = pc.describe_index(name=index_name)\n",
    "print(f\"The index host is {index_config.host}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8eb7f2d1-3027-4748-a135-23b12620c6ac",
   "metadata": {},
   "source": [
    "## Working with the Index\n",
    "\n",
    "Data operations such as `upsert` and `query` are sent directly to the index host instead of `api.pinecone.io`, so we use a different client object object for these operations. By using the `.Index()` helper method to construct this client object, it will automatically inherit your API Key and any other configurations from the parent `Pinecone` instance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "blvbpzBAxPJO",
   "metadata": {
    "id": "blvbpzBAxPJO",
    "papermill": {
     "duration": 0.869129,
     "end_time": "2021-04-16T15:12:42.358177",
     "exception": false,
     "start_time": "2021-04-16T15:12:41.489048",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Instantiate an index client\n",
    "index = pc.Index(host=index_config.host)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "improved-season",
   "metadata": {
    "id": "improved-season",
    "papermill": {
     "duration": 0.038949,
     "end_time": "2021-04-16T15:12:42.437637",
     "exception": false,
     "start_time": "2021-04-16T15:12:42.398688",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Generate movie data\n",
    "\n",
    "For this simple example scenario, we will make up some small vectors to represent different movies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "disciplinary-district",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 175
    },
    "id": "disciplinary-district",
    "outputId": "25708b6c-92c4-4fd0-be92-dcd67d75a48d",
    "papermill": {
     "duration": 0.24115,
     "end_time": "2021-04-16T15:12:42.715499",
     "exception": false,
     "start_time": "2021-04-16T15:12:42.474349",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>vector</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Wall-E</td>\n",
       "      <td>[1.0, 1.0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Up</td>\n",
       "      <td>[2.0, 2.0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ratatouille</td>\n",
       "      <td>[3.0, 3.0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Toy Story</td>\n",
       "      <td>[4.0, 4.0]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            id      vector\n",
       "0       Wall-E  [1.0, 1.0]\n",
       "1           Up  [2.0, 2.0]\n",
       "2  Ratatouille  [3.0, 3.0]\n",
       "3    Toy Story  [4.0, 4.0]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Generate some data\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.DataFrame()\n",
    "df[\"id\"] = [\"Wall-E\", \"Up\", \"Ratatouille\", \"Toy Story\"]\n",
    "df[\"vector\"] = [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0]]\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "leading-flesh",
   "metadata": {
    "id": "leading-flesh",
    "papermill": {
     "duration": 0.030901,
     "end_time": "2021-04-16T15:12:42.777653",
     "exception": false,
     "start_time": "2021-04-16T15:12:42.746752",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Insert vectors without specifying a namespace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "nearby-skiing",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "nearby-skiing",
    "outputId": "db1c8819-86bd-49db-fadb-3f175938c1a1",
    "papermill": {
     "duration": 1.65623,
     "end_time": "2021-04-16T15:12:44.464926",
     "exception": false,
     "start_time": "2021-04-16T15:12:42.808696",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'upserted_count': 4}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Insert vectors without specifying a namespace\n",
    "index.upsert(vectors=zip(df.id, df.vector))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9640eacd-85bc-4889-8500-e1e5555bd21c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'dimension': 2,\n",
       " 'index_fullness': 0.0,\n",
       " 'metric': 'euclidean',\n",
       " 'namespaces': {'': {'vector_count': 4}},\n",
       " 'total_vector_count': 4,\n",
       " 'vector_type': 'dense'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "\n",
    "def is_fresh(index):\n",
    "    stats = index.describe_index_stats()\n",
    "    vector_count = stats.total_vector_count\n",
    "    return vector_count > 0\n",
    "\n",
    "\n",
    "while not is_fresh(index):\n",
    "    # It takes a few moments for vectors we just upserted\n",
    "    # to become available for querying\n",
    "    time.sleep(5)\n",
    "\n",
    "# View index stats\n",
    "index.describe_index_stats()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "large-tunisia",
   "metadata": {
    "id": "large-tunisia",
    "papermill": {
     "duration": 0.033445,
     "end_time": "2021-04-16T15:12:44.537290",
     "exception": false,
     "start_time": "2021-04-16T15:12:44.503845",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Insert vectors into a namespace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "excellent-channel",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 112
    },
    "execution": {
     "iopub.execute_input": "2021-04-16T15:12:44.610746Z",
     "iopub.status.busy": "2021-04-16T15:12:44.609058Z",
     "iopub.status.idle": "2021-04-16T15:12:44.612765Z",
     "shell.execute_reply": "2021-04-16T15:12:44.612174Z"
    },
    "id": "excellent-channel",
    "outputId": "7d790f07-b893-4384-abf6-78a7fe5bbad2",
    "papermill": {
     "duration": 0.043725,
     "end_time": "2021-04-16T15:12:44.612991",
     "exception": false,
     "start_time": "2021-04-16T15:12:44.569266",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "  <div id=\"df-119e686d-3a1c-4f9f-8f79-d45ba1e6118f\">\n",
       "    <div class=\"colab-df-container\">\n",
       "      <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>vector</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Wall-E</td>\n",
       "      <td>[1.0, 1.0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ratatouille</td>\n",
       "      <td>[3.0, 3.0]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-119e686d-3a1c-4f9f-8f79-d45ba1e6118f')\"\n",
       "              title=\"Convert this dataframe to an interactive table.\"\n",
       "              style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "       width=\"24px\">\n",
       "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
       "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
       "  </svg>\n",
       "      </button>\n",
       "\n",
       "\n",
       "\n",
       "    <div id=\"df-bf574b15-141a-4290-9a51-75b2c2a2c150\">\n",
       "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-bf574b15-141a-4290-9a51-75b2c2a2c150')\"\n",
       "              title=\"Suggest charts.\"\n",
       "              style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "      </button>\n",
       "    </div>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "    background-color: #E8F0FE;\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: #1967D2;\n",
       "    height: 32px;\n",
       "    padding: 0 0 0 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: #E2EBFA;\n",
       "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: #174EA6;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "    background-color: #3B4455;\n",
       "    fill: #D2E3FC;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart:hover {\n",
       "    background-color: #434B5C;\n",
       "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "    fill: #FFFFFF;\n",
       "  }\n",
       "</style>\n",
       "\n",
       "    <script>\n",
       "      async function quickchart(key) {\n",
       "        const containerElement = document.querySelector('#' + key);\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      }\n",
       "    </script>\n",
       "\n",
       "      <script>\n",
       "\n",
       "function displayQuickchartButton(domScope) {\n",
       "  let quickchartButtonEl =\n",
       "    domScope.querySelector('#df-bf574b15-141a-4290-9a51-75b2c2a2c150 button.colab-df-quickchart');\n",
       "  quickchartButtonEl.style.display =\n",
       "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "}\n",
       "\n",
       "        displayQuickchartButton(document);\n",
       "      </script>\n",
       "      <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      flex-wrap:wrap;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "      <script>\n",
       "        const buttonEl =\n",
       "          document.querySelector('#df-119e686d-3a1c-4f9f-8f79-d45ba1e6118f button.colab-df-convert');\n",
       "        buttonEl.style.display =\n",
       "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "        async function convertToInteractive(key) {\n",
       "          const element = document.querySelector('#df-119e686d-3a1c-4f9f-8f79-d45ba1e6118f');\n",
       "          const dataTable =\n",
       "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                     [key], {});\n",
       "          if (!dataTable) return;\n",
       "\n",
       "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "            + ' to learn more about interactive tables.';\n",
       "          element.innerHTML = '';\n",
       "          dataTable['output_type'] = 'display_data';\n",
       "          await google.colab.output.renderOutput(dataTable, element);\n",
       "          const docLink = document.createElement('div');\n",
       "          docLink.innerHTML = docLinkHtml;\n",
       "          element.appendChild(docLink);\n",
       "        }\n",
       "      </script>\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "text/plain": [
       "            id      vector\n",
       "0       Wall-E  [1.0, 1.0]\n",
       "2  Ratatouille  [3.0, 3.0]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "romantic_comedies = [\"Wall-E\", \"Ratatouille\"]\n",
    "romcom_df = df[df.id.isin(romantic_comedies)]\n",
    "romcom_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "registered-eagle",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2021-04-16T15:12:44.686462Z",
     "iopub.status.busy": "2021-04-16T15:12:44.685559Z",
     "iopub.status.idle": "2021-04-16T15:12:44.891495Z",
     "shell.execute_reply": "2021-04-16T15:12:44.890307Z"
    },
    "id": "registered-eagle",
    "outputId": "6a57ed6b-43cb-49f0-b85e-917e4a5869d2",
    "papermill": {
     "duration": 0.244898,
     "end_time": "2021-04-16T15:12:44.891970",
     "exception": false,
     "start_time": "2021-04-16T15:12:44.647072",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'dimension': 2,\n",
       " 'index_fullness': 0.0,\n",
       " 'namespaces': {'': {'vector_count': 4},\n",
       "                'romantic-comedy': {'vector_count': 2}},\n",
       " 'total_vector_count': 6}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Insert vectors into a namespace for romantic comedies\n",
    "index.upsert(vectors=zip(romcom_df.id, romcom_df.vector), namespace=\"romantic-comedy\")\n",
    "index.describe_index_stats()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eight-sixth",
   "metadata": {
    "id": "eight-sixth",
    "papermill": {
     "duration": 0.032846,
     "end_time": "2021-04-16T15:12:44.963008",
     "exception": false,
     "start_time": "2021-04-16T15:12:44.930162",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Query top-3 results, without a namespace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "timely-allen",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2021-04-16T15:12:45.035032Z",
     "iopub.status.busy": "2021-04-16T15:12:45.034363Z",
     "iopub.status.idle": "2021-04-16T15:12:45.147905Z",
     "shell.execute_reply": "2021-04-16T15:12:45.146636Z"
    },
    "id": "timely-allen",
    "outputId": "3b1d65b7-4809-4c5e-db23-1a44d2fc8079",
    "papermill": {
     "duration": 0.152593,
     "end_time": "2021-04-16T15:12:45.148326",
     "exception": false,
     "start_time": "2021-04-16T15:12:44.995733",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'matches': [{'id': 'Wall-E', 'score': 0.0, 'values': []},\n",
       "             {'id': 'Up', 'score': 1.99999905, 'values': []},\n",
       "             {'id': 'Ratatouille', 'score': 7.99999809, 'values': []}],\n",
       " 'namespace': ''}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_results = index.query(vector=df[df.id == \"Wall-E\"].vector[0], top_k=3)\n",
    "query_results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "arabic-shooting",
   "metadata": {
    "id": "arabic-shooting",
    "papermill": {
     "duration": 0.034931,
     "end_time": "2021-04-16T15:12:45.223865",
     "exception": false,
     "start_time": "2021-04-16T15:12:45.188934",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Query top-3 results, with a namespace\n",
    "\n",
    "We should expect to see only romantic comedies in the query results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "stuck-hardware",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2021-04-16T15:12:45.302733Z",
     "iopub.status.busy": "2021-04-16T15:12:45.300355Z",
     "iopub.status.idle": "2021-04-16T15:12:45.411744Z",
     "shell.execute_reply": "2021-04-16T15:12:45.410749Z"
    },
    "id": "stuck-hardware",
    "outputId": "fe37ce47-ace9-4835-9c2a-5f245ddfb441",
    "papermill": {
     "duration": 0.151954,
     "end_time": "2021-04-16T15:12:45.412130",
     "exception": false,
     "start_time": "2021-04-16T15:12:45.260176",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'matches': [{'id': 'Wall-E', 'score': 0.0, 'values': []},\n",
       "             {'id': 'Ratatouille', 'score': 7.99999809, 'values': []}],\n",
       " 'namespace': 'romantic-comedy'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_results = index.query(\n",
    "    vector=df[df.id == \"Wall-E\"].vector[0], top_k=3, namespace=\"romantic-comedy\"\n",
    ")\n",
    "query_results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "geological-competition",
   "metadata": {
    "id": "geological-competition",
    "papermill": {
     "duration": 0.035328,
     "end_time": "2021-04-16T15:12:45.490265",
     "exception": false,
     "start_time": "2021-04-16T15:12:45.454937",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Delete the index\n",
    "\n",
    "Once we're done, delete the index to save resources."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "valuable-rehabilitation",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-04-16T15:12:45.567258Z",
     "iopub.status.busy": "2021-04-16T15:12:45.566027Z",
     "iopub.status.idle": "2021-04-16T15:12:58.139410Z",
     "shell.execute_reply": "2021-04-16T15:12:58.138501Z"
    },
    "id": "valuable-rehabilitation",
    "papermill": {
     "duration": 12.613954,
     "end_time": "2021-04-16T15:12:58.139886",
     "exception": false,
     "start_time": "2021-04-16T15:12:45.525932",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Delete the index\n",
    "pc.delete_index(name=index_name)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 51.37705,
   "end_time": "2021-04-16T15:12:58.702015",
   "environment_variables": {},
   "exception": null,
   "input_path": "/notebooks/quick_tour/namespacing.ipynb",
   "output_path": "/notebooks/tmp/quick_tour/namespacing.ipynb",
   "parameters": {},
   "start_time": "2021-04-16T15:12:07.324965",
   "version": "2.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
